<ul id="main_menu" class="header_menu ">
        {assign var=startdepth value=0}
        {assign var=first_menu value=1}
        {foreach name="children" key=key from=$sections item=section}
        {assign var=nextkey value=`$key+1`}
        {assign var=previouskey value=`$key-1`}

        {if $sections[$previouskey]->depth < $section->depth && $smarty.foreach.children.first!=true}
        <ul class="menu_child" id="menu_child_{$section->parent}">
        {/if}

        {if $section->active == 1}
            <li class="{if $first_menu==1}first {assign var=first_menu value=0}{/if}{if $section->depth ==0}top_menu {/if}{if $section->id ==$sections[$nextkey]->parent}have_child {else}no_child {/if}{if $section->id==$current->id}current_menu {/if}" rel="{$section->id}" >
            <a href="{$section->link}" {if $section->new_window ==1}target=_blank{/if}>
                {if $section->parent ==0}
                <span class="iconic {$section->css_class}"></span> 
                {/if}
                {$section->name}
            </a>
            {if $sections[$nextkey]->depth == $section->depth}</li>{/if}
        {else }
            <li>
                <a href="#">{$section->name}</a>
            {if $sections[$nextkey]->depth == $section->depth}</li>{/if}
        {/if}

        {if $sections[$nextkey]->depth < $section->depth}
            {if $smarty.foreach.children.last==true}
                {assign var=nextdepth value=$startdepth}
            {else}
                {assign var=nextdepth value=$sections[$nextkey]->depth}
            {/if}
            {math equation="x-y" x=$section->depth y=$nextdepth assign=looper}
            {section name="close" loop=$looper}
                        </li>
                    </ul>

            {/section}
                </li>
        {/if}

        {/foreach}
    </ul>
<div class="clearfix"></div>